package com.hs.srm.api.modules.sampleRequest;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.hs.srm.admin.modules.pricemanagement.entity.InquiryQuotationEntity;
import com.hs.srm.admin.modules.requisition.entity.SampleOrderReportEntity;
import com.hs.srm.admin.modules.requisition.service.SampleOrderReportService;
import com.hs.srm.common.utils.DateUtils;
import com.hs.srm.common.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.xml.crypto.Data;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;

/**
 * 样品报告审核结果
 * <p>
 * 传递参数：
 * pkId : 报价单id（必选参数）
 * result : 审核结果，剩余原料处理意见(1,退还供应商 2,正常使用 3,报废 )
 * msg : 审核意见（可选参数）
 * 参数示例
 * 审核通过：{"quotationId":"123456","result":1, "msg":"同意"}
 * 审核不通过：{"quotationId":"123456","result":false, "msg":"价格太高，不同意"}
 * 客户端调用示例：
 * String JSONString = "{\"quotationId\":\"123456\",\"result\":true, \"msg\":\"同意\"}";
 * HttpClient client = new HttpClient("http://127.0.0.1:8085/hssrm/api/parity/shenheResult");
 * Response response = client.post(new Parameter("jsonData", JSONString));
 * <p>
 * {"result":true,"msg":"通过","inquiryId":"b77f38ef60854a89a5458bde13bc1a27",
 * "quotationId":"38ddde5a196a40ed88385676ddf80147","processId":"16c0958dd0e6157452158df4b89a2cf6"}
 */
@RequestMapping("/api/sampleOrderApi")
@EnableAutoConfiguration
@Controller
public class SampleOrderApiController {
    @Autowired
    private SampleOrderReportService sampleOrderReportService;

    @RequestMapping(value = "/sampleShenheResult", method = RequestMethod.POST)
    @ResponseBody
    public R shenheResult(String jsonData) {
        JSONObject jsonObject = JSON.parseObject(jsonData);
        if (jsonObject != null) {
            String oaDocId = jsonObject.getString("processId");
            String trial_idea = jsonObject.getString("trial_idea");//产品意见
            String comprehensive_assessment = jsonObject.getString("comprehensive_assessment");
            String material_idea = jsonObject.getString("material_idea");//剩余原料意见
            String sqe_date = jsonObject.getString("date");//完成时间
            String estimate_date = jsonObject.getString("estimate_date");//预计完成时间
            SampleOrderReportEntity sampleOrderReportEntity = this.sampleOrderReportService.selectOne(new EntityWrapper<SampleOrderReportEntity>().eq("oa_doc_id", oaDocId));
            sampleOrderReportEntity.setComprehensiveAssessment(comprehensive_assessment);
            sampleOrderReportEntity.setMaterialIdea(material_idea);
            sampleOrderReportEntity.setTrialIdea(trial_idea);
            SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
            try {
                if (sqe_date != null) {
                    Date date = df.parse(sqe_date);
                    Date data1 = df.parse(estimate_date);
                    sampleOrderReportEntity.setSqeDate(date);
                    sampleOrderReportEntity.setEstimatedFinishTime(data1);
                }

            } catch (ParseException e) {
                e.printStackTrace();
            }
            this.sampleOrderReportService.update(sampleOrderReportEntity, new EntityWrapper<SampleOrderReportEntity>().eq("oa_doc_id", oaDocId));
        }

        return R.ok();
    }
}




